package com.springboot.controller;

import com.springboot.model.AssistantMessageInform;
import com.springboot.model.AssistantMessageInformKey;
import com.springboot.service.AssistantSetReadNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

@Controller
public class AssistantSetReadNotice {
    @Autowired
    AssistantSetReadNoticeService assistantSetReadNoticeService;

    @CrossOrigin
    @RequestMapping(value = "/assistantSetReadNotice")
    public @ResponseBody
    String setReadNotice(@RequestParam("messageId") Integer messageId,
                         @RequestParam("assistantId") Integer assistantId){
        System.out.println("设置已读");
        try{
            AssistantMessageInformKey assistantMessageInformKey=new AssistantMessageInformKey(assistantId,messageId);
            AssistantMessageInform assistantMessageInform=new AssistantMessageInform();
            assistantMessageInform=assistantSetReadNoticeService.selectById(assistantMessageInformKey);
            assistantMessageInform.setMessageState3("已读");
            int result=-1;
            result=assistantSetReadNoticeService.updateRead(assistantMessageInform);
            System.out.println(result+" "+assistantMessageInform.getAssistantId()+" "+assistantMessageInform.getMessageState3());
            return "该消息已读";
        }
        catch (Exception e){
            return "错误";
        }
    }

    @CrossOrigin
    @RequestMapping(value = "/assistantSetAllReadNotice")
    public @ResponseBody
    String setReadNotice(@RequestParam("assistantId") Integer assistantId){
        System.out.println("设置一键已读");
        try{
            ArrayList<AssistantMessageInform> assistantMessageInforms=new ArrayList<>();
            assistantMessageInforms=assistantSetReadNoticeService.selectAllNoReadByAssistantId(assistantId);
            int a=assistantMessageInforms.size();
            System.out.println(a);
            for(int i=0;i<a;i++)
            {
                AssistantMessageInform assistantMessageInform=new AssistantMessageInform();
                assistantMessageInforms.get(i).setMessageState3("已读");
                assistantMessageInform=assistantMessageInforms.get(i);
                int result=-1;
                result=assistantSetReadNoticeService.updateRead(assistantMessageInform);
                System.out.println(result+" "+assistantMessageInform.getAssistantId()+" "+assistantMessageInform.getMessageState3());
            }
            return "All已读";
        }
        catch (Exception e){
            return "错误";
        }
    }
}
